package com.lzy.wzy.listener;

import com.lzy.wzy.constant.MyConstant;
import com.lzy.wzy.mapper.UserMapper;
import com.lzy.wzy.mapper.WorkMapper;
import com.lzy.wzy.model.CloseAndAntiClose;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Component(value = "taskListener")
@Slf4j
public class TestTaskHandler implements ExecutionListener {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WorkMapper workMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notify(DelegateExecution delegateExecution) {
        boolean result = (boolean) delegateExecution.getVariable("result");
        String workOrderNumber = (String) delegateExecution.getVariable("workOrderNumber");
        String processInstanceId = delegateExecution.getProcessInstanceId();
        String key = (String) delegateExecution.getVariable("key");
        if (result) {
            successHandel(key, processInstanceId, workOrderNumber);
            log.info("通过");
        } else {
            reject(key, workOrderNumber, processInstanceId);
            log.info("拒绝");
        }
    }

    private void reject(String key, String workOrderNumber, String processInstanceId) {
        switch (key) {
            case "product":
                userMapper.updateProApproveStatus(MyConstant.ApproveStatus.REVIEW_REJECTED,MyConstant.ApproveStatus.REVIEW_REJECTED,processInstanceId,0);
                log.info("生产驳回，流程结束{}",processInstanceId);
                break;
            case "qua":
                userMapper.updatePsApproveStatus(MyConstant.ApproveStatus.REVIEW_REJECTED,MyConstant.ApproveStatus.MODERATED,processInstanceId);
                log.info("质检审核驳回{}",processInstanceId);
                break;
            case "craft":
                userMapper.updateCraApproveStatus(MyConstant.ApproveStatus.REVIEW_REJECTED,MyConstant.ApproveStatus.MODERATED,processInstanceId);
                log.info("工艺驳回，流程结束{}",processInstanceId);
                break;
            case "quality":
                userMapper.updateQalApproveStatus(MyConstant.ApproveStatus.REVIEW_REJECTED,MyConstant.ApproveStatus.MODERATED,processInstanceId);
                caTicketStatus(workOrderNumber,MyConstant.WorkOrderStatus.InSystem,MyConstant.WorkOrderStatus.FINISHED);
                log.info("质量驳回，流程结束{}",processInstanceId);
                break;
            case "plan":
                userMapper.updateBlApproveStatus(MyConstant.ApproveStatus.REVIEW_REJECTED,MyConstant.ApproveStatus.MODERATED,processInstanceId);
                caTicketStatus(workOrderNumber,MyConstant.WorkOrderStatus.CASECLOSE,MyConstant.WorkOrderStatus.FINISHED);
                break;
        }
    }


    private void caTicketStatus(String workOrderNumber, String futureStatus, String expectedStatus) {
        workMapper.updateTicketStatus(workOrderNumber, futureStatus, expectedStatus);
    }

    private void successHandel(String key, String processInstanceId, String workOrderNumber) {

        switch (key) {
            case "product":
                //更新审批状态
                userMapper.updateProApproveStatus(MyConstant.ApproveStatus.EXAMINATION_PASSED,MyConstant.ApproveStatus.UNDER_REVIEW,processInstanceId,1);
                log.info("生产审核通过{}",processInstanceId);
                break;
            case "qua":
                userMapper.updatePsApproveStatus(MyConstant.ApproveStatus.EXAMINATION_PASSED,MyConstant.ApproveStatus.EXAMINATION_PASSED,processInstanceId);
                log.info("质检审核通过{}",processInstanceId);
                break;
            case "quality":
                userMapper.updateQalApproveStatus(MyConstant.ApproveStatus.EXAMINATION_PASSED,MyConstant.ApproveStatus.EXAMINATION_PASSED,processInstanceId);
                workMapper.updateStatusAndCompletionDate(MyConstant.WorkOrderStatus.FINISHED, MyConstant.WorkOrderStatus.InSystem, LocalDate.now(), workOrderNumber);
                log.info("质量审核通过{}",processInstanceId);
                break;
            case "craft":
                userMapper.updateCraApproveStatus(MyConstant.ApproveStatus.EXAMINATION_PASSED,MyConstant.ApproveStatus.EXAMINATION_PASSED,processInstanceId);
                log.info("工艺审核通过{}",processInstanceId);
                break;
            case "plan":
                LocalDate now = LocalDate.now();
                userMapper.updatePlApproveStatus(MyConstant.ApproveStatus.EXAMINATION_PASSED,MyConstant.ApproveStatus.EXAMINATION_PASSED,processInstanceId);
                workMapper.updateStatusAndClosingTime(MyConstant.WorkOrderStatus.CASECLOSE, MyConstant.WorkOrderStatus.FINISHED, now, now, workOrderNumber);
                log.info("计划结案{}",processInstanceId);
                break;
        }
    }
}
